package com.yangxinxin.lesson.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;



/** @Author yangxinxin
 * @Description //TODO
 * @Date 17:10 2020/2/14
 * @Param
 * @return
 **/
@Service
//1.获取实例需要从jedisPool中获取
//2.用完jedis实例需要归还给jedisPool
//3.如果jedis在使用过程中出错，则也需要归还给jedisPool[不归还会造成内存泄露]
public class RedisService {
    @Autowired
    private JedisPool jedisPool;

    public boolean exists(String key){
        Jedis jedis = null;//声明一个jedis实例对象
        boolean result;
        try {
            jedis = jedisPool.getResource();//take from JedisPool
            result = jedis.exists(key);
        } finally {
            if(jedis != null){
                jedis.close();
            }
        }
        return  result;
    }
    public Long del(final String... keys){

        Jedis jedis = null;//声明一个jedis实例对象
        Long result;//结果
        try {
            jedis  = jedisPool.getResource();//take resource from JedisPool
            result = jedis.del(keys);
        } finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return  result;
    }
}
